$(document).ready(function () {
  var page = $(".page");
  var count = page.data("count");
  var current = page.data("current");
  var ajax = page.data("ajax") || false;
  var url =  "?";
  
  var urlMsg = getURLInformation();

  $(".job-location a").each(function(){
    setJoinBarUnderlineAndHref(this,urlMsg)
  });

  $(".job-class a").each(function(){
    setJoinBarUnderlineAndHref(this,urlMsg)
  });

  $(".join-bar i").hover(function(){
    if(!$(this).hasClass("selected")){
     $(this).addClass("cur");
    }
  },function(){
    if(!$(this).hasClass("selected")){
      $(this).removeClass("cur");
    }
  })

  hotUp();

  if(page[0]){
    pageChange(page, count, current, ajax,null,setPageChangeUrl(urlMsg,url));
  }
  

  $(".job-list-info").click(function(){
    $(this).parents("li").siblings("li").removeClass("show").find(".job-list-more").addClass("hide");
    showOrHide($(this).siblings(".job-list-more")[0],["hide"]);
    showOrHide($(this).parent("li")[0],["show"]);
  });

  $(".hidemore").click(function(){
    showOrHide($(this).parent(".job-list-more")[0],["hide"]);
    showOrHide($(this).parents("li")[0],["show"]);
  });
  if(!!window.location.search){
    $(window).scrollTop($(".join-bar").offset().top)
  }
});

function hotUp(){
  var $list = $(".job-list-ul li[data-hot=1]");
  for(var i = $list.length - 1  ; i >= 0 ; i --){
    $list.eq(i).prependTo($(".job-list-ul"));
  }
}

function setPageChangeUrl(urlMsg,url){
  for(var x in urlMsg){
    if(x != "page" && x != undefined){
      url +=  x + "=" + urlMsg[x] + "&";
    } 
  }
  return url;
}

function setJoinBarUnderlineAndHref(dom,urlMsg){
  var url = "?";
  if(dom.dataset){
    if(dom.dataset[dom.dataset.type] == decodeURI(urlMsg[dom.dataset.type])){
      $(dom).parent("i").addClass("cur selected");
    }
    for(var x in urlMsg){
      if(x != "page" && x != dom.dataset.type && x != ""){
        url  +=  x + "=" + urlMsg[x] + "&";
      }
    }
    dom.href = url + dom.dataset.type + "=" + encodeURIComponent(dom.dataset[dom.dataset.type]) ;   
  }else{
    var myDataset =  getDataset(dom);
    if(myDataset[myDataset.type]== decodeURI(urlMsg[myDataset.type])){
      $(dom).parent("i").addClass("cur selected");
    }
    for(var x in urlMsg){
      if(x != "page" && x != myDataset.type && x != ""){
        url  +=  x + "=" + urlMsg[x] + "&";
      }
    }
    dom.href = url + myDataset.type + "=" + encodeURIComponent(myDataset[myDataset.type]) ;   
  }
}

function showOrHide(dom,classNameList){
  for(var i = 0 ; i < classNameList.length ; i ++){
    if($(dom).hasClass(classNameList[i])){
      $(dom).removeClass(classNameList[i]);
    }else{
      $(dom).addClass(classNameList[i]);
    }
  }
}

function getURLInformation() {
  var urlMsg = {}; //定义一个空对象urlMsg
  if (window.location.href.split('#')[0].split('?')[1]) {
      var urlSearch = window.location.href.split('#')[0].split('?')[1].split('&');
  }
  if (urlSearch) {
      for (var i = 0; i < urlSearch.length; i++) {
          urlMsg[urlSearch[i].split('=')[0]] = urlSearch[i].split('=')[1] || "";
      }
  }
  return urlMsg;
}

function getDataset(dom){
  var myDataset = {};
  $.each(dom.attributes,function(i,attrib){  
      if(attrib.name && attrib.name.indexOf("data-") > -1){
        myDataset[attrib.name.replace("data-","")] = attrib.value;
      }
  });  
  return myDataset;
}